﻿
using Dapper;
using System;
using System.Data;
using System.Linq;
using static Dapper.SqlMapper;
namespace hdal {
    internal abstract class AbsSequenceIdServer : AbsWMSBaseServer, ISequenceIdServer
    {
        static long SEQUENCES_ID = 0;
        static int index = 0;
        public long getId()
        {
            lock (this)
            {
                long id = 0;
                if (SEQUENCES_ID == 0 || index == 99)
                {
                    SEQUENCES_ID = querySequence(null,null);
                    index = 0;
                }
                index++;
                id = SEQUENCES_ID * 100 + index;
                return id;
            }
        }
        public virtual DateTime GetTime()
        {
            return Connection.Query<DateTime>("select getdate()").Single();
        }
       
        public abstract long querySequence(IDbConnection connection ,IDbTransaction transaction);
         public long getId(IDbConnection connection ,IDbTransaction transaction) {
            lock( this ) {
                long id = 0;
                if( SEQUENCES_ID == 0 || index == 99 ) {
                    SEQUENCES_ID = querySequence(connection,transaction);
                    index = 0;
                }
                index++;
                id = SEQUENCES_ID * 100 + index;
                return id;
            }
        }
    }
}
